using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using WODPL.Models;


namespace WODPL.GameComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameMenuItem : DrawableGameComponent
    {
        private readonly SpriteFont spriteFont;
        private SpriteBatch spriteBatch;
        private readonly GameMenuItemModel model;
        private Vector2 bounds;
        private Vector2 modelPositionOnScreen;
        private bool mouseInItem;
        private readonly SelectionModel selectionModel;
        private MouseState prevMouseState = Mouse.GetState();
        private MouseState mouseState = Mouse.GetState();
        private Rectangle rectangle;
        private Vector2 position;

        public GameMenuItem(Game game, GameMenuItemModel model, SelectionModel selectionModel,Vector2 position,SpriteFont spriteFont)
            : base(game)
        {
            this.selectionModel = selectionModel;
            this.model = model;
            this.position = position;
            this.spriteFont = spriteFont;
        }

        public override void Initialize()
        {
            base.Initialize();
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            bounds = spriteFont.MeasureString(model.Text);
            modelPositionOnScreen = position * new Vector2(Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height)- bounds/2;
            rectangle = new Rectangle((int)modelPositionOnScreen.X, (int)modelPositionOnScreen.Y, (int)bounds.X, (int)bounds.Y);
        }

        protected override void LoadContent()
        {
            base.LoadContent();         
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            spriteBatch.Begin();
            spriteBatch.DrawString(spriteFont, model.Text, modelPositionOnScreen,
                                    (selectionModel.IsSelected(model)) ? Color.Yellow : Color.Blue);
            spriteBatch.End();
        }

        public override void Update(GameTime gameTime)
        {   
            base.Update(gameTime);
            mouseState = Mouse.GetState();
            if ((prevMouseState.X != mouseState.X) || (prevMouseState.Y != mouseState.Y))
            {
                if (rectangle.Contains(Mouse.GetState().X, Mouse.GetState().Y))
                {
                    selectionModel.SelectModel(model);
                    mouseInItem = true;
                }
                else if (mouseInItem)
                {
                    selectionModel.SelectModel(null);
                    mouseInItem = false;
                }
            }

            KeyboardState state = Keyboard.GetState();
            prevMouseState = mouseState;
            if (state.IsKeyDown(Keys.Enter) ||
                mouseState.LeftButton == ButtonState.Pressed && selectionModel.IsSelected(model))
            {
                model.PerformAction();
            }
        }
    }
}